package cn.zl.systema.controller;

import cn.zl.ssoserver.constant.ServerConstant;
import cn.zl.ssoserver.util.SsoCookieUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Description:控制器
 * @Author: zllwsy
 * @Date: 2020/11/4 10:42
 */
@Controller
public class HomeController {
    @Autowired
    private RestTemplate restTemplate;
    @RequestMapping("/home")
    public String goHome(HttpSession session, Model model,HttpServletRequest request){
        //获取数据，跳转页面，说实话，很久没写过跳转页面了，都是返回json数据
        Object userInfo = session.getAttribute("userInfo");
        System.out.println(userInfo);
        model.addAttribute("userInfo",session.getAttribute("userInfo"));
        return "home";
    }

    @ResponseBody
    @RequestMapping("/logout")
    @SuppressWarnings("all")
    public String logout(HttpServletRequest request, HttpServletResponse response) {
        //获取ticket
        String ticket = SsoCookieUtil.getCookie(request,"ticket");
        //请求sso-server退出登录
        boolean isLogout = restTemplate.getForObject(ServerConstant.SSO_URL+"logout?ticket="+ticket,Boolean.class);
        if(isLogout){
            //清除cookie
            SsoCookieUtil.setCookie(response,"ticket","",0);
            return "用户已登出";
        }
        return "登出失败";
    }
}
